String clazzTpl({package}) => """
import 'package:redux/redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:$package/debug/app_debug_model.dart';
import 'package:$package/store/theme/theme_state.dart';
import 'package:$package/store/redux/redux_state.dart';
import 'package:$package/store/app_config/app_config_state.dart';

export 'package:redux/redux.dart';
export 'package:flutter_redux/flutter_redux.dart';
export 'package:$package/store/redux/redux_state.dart';
export 'package:$package/store/theme/theme_state.dart';

class BaseStoreContainer extends StatelessWidget {
  final void Function(Store<ReduxState>)? onInit;
  final void Function(Store<ReduxState>)? onInitialBuild;
  final void Function(Store<ReduxState>? old, Store<ReduxState> now)?
  onWillChange;
  final void Function(Store<ReduxState>? old, Store<ReduxState> now)?
  onDidChange;
  final void Function(Store<ReduxState>)? onDispose;
  final bool Function(ReduxState)? ignoreChange;
  final Widget Function(BuildContext context, ThemeState theme,
      ReduxState state, dynamic dispatch)? builder;
  final bool distinct;
  final bool rebuildOnChange;

  BaseStoreContainer({
    Key? key,
    this.onInit,
    this.onInitialBuild,
    this.onWillChange,
    this.onDidChange,
    this.onDispose,
    this.ignoreChange,
    this.distinct = false,
    this.rebuildOnChange = true,
    this.builder,
  }) : super(key: key) {
    assert(builder != null);
  }

  @override
  Widget build(BuildContext context) {
    return StoreConnector<ReduxState, Store<ReduxState>>(
      onInit: (store) {
        StaticStore.store = store;
        if (onInit != null) onInit!(store);
      },
      distinct: distinct,
      onWillChange: onWillChange,
      onDidChange: onDidChange,
      onInitialBuild: onInitialBuild,
      onDispose: onDispose,
      rebuildOnChange: rebuildOnChange,
      ignoreChange: ignoreChange,
      converter: (store) => store,
      builder: (context, store) {
        return builder!(
            context, store.state.themeState!, store.state, store.dispatch);
      },
    );
  }
}

class StaticStore {
  static Store<ReduxState>? store;
  static Map<String, dynamic> get appInfo {
    final Map<String, dynamic> headers = <String, dynamic>{};

    /// 平台信息
    headers['device_id'] = AppAndDevice.deviceId;
    headers['device_type'] = AppAndDevice.deviceType;
    headers['device_version'] = AppAndDevice.deviceVersion;
    headers['device_display'] = AppAndDevice.deviceDisplay;
    headers['device_physical'] = AppAndDevice.isPhysicalDevice;
    headers['device_manufacturer'] = AppAndDevice.deviceManufacturer;

    ///app信息
    headers['app_version'] = AppAndDevice.appVersion;
    headers['app_buildNumber'] = AppAndDevice.appBuildNumber;
    headers['app_package'] = AppAndDevice.appPackage;

    //后端统计
    headers['req-type'] = "app";

    return headers;
  }
}

extension BuildContextEx on BuildContext {
  bool get isDev =>
      StaticStore.store!.state.appConfigState!.findAppDebug(AppDebugKey.dev);

  bool isOn(AppDebugKey key) =>
      StaticStore.store!.state.appConfigState!.findAppDebug(key);

  ThemeState get theme => StaticStore.store!.state.themeState!;

  String get themeString => theme.brightness.toString().split(".").last;

  ReduxState get state => StaticStore.store!.state;

  dynamic get dispatch => StaticStore.store!.dispatch;

  String get host => StaticStore.store!.state.appConfigState!.findAppHost();

  String get h5FaceHost =>
      StaticStore.store!.state.appConfigState!.findH5FaceHost();

  String get hostEnv =>
      StaticStore.store!.state.appConfigState!.findAppHostEnv();

  bool get isTestEnv =>
      StaticStore.store!.state.appConfigState!.isTestAppHostEnv();

  // 不是正式环境，其他所有环境返回false
  bool get isProdEnv =>
      StaticStore.store!.state.appConfigState!.isProdAppHostEnv();

  // 只有测试环境是T000003，其它环境都是T000002
  String get tenantId => isTestEnv ? "T000003" : "T000002";

  Map<String, dynamic> get appInfo => StaticStore.appInfo;

  String get domain =>
      host.replaceAll('cloudapi', 'syclient').replaceAll('syapi', 'client');

  // 根据日期控制是否在审核期间生效
  bool get isReviewApp {
    final now = DateTime.now();
    final reviewTime = DateTime(2022, 3, 22);
    return now.difference(reviewTime).inDays < 4;
  }
}
""";
